package com.taxi.driver.module.order.complain;

import android.text.TextUtils;

import com.qianxx.utils.RxUtil;
import com.qianxx.utils.TypeUtils;
import com.taxi.driver.R;
import com.taxi.driver.common.BasePresenter;
import com.taxi.driver.data.order.OrderRepository;
import com.taxi.driver.data.user.UserRepository;
import com.taxi.driver.module.vo.ComplainVO;

import javax.inject.Inject;

/**
 * 功能描述：TODO 逻辑待优化
 */
public class OrderComplainPresenter extends BasePresenter implements OrderComplainContract.Presenter {

    OrderComplainContract.View mView;
    OrderRepository mOrderRepository;
    UserRepository mUserRepository;
    String mOrderUuid;

    @Inject
    public OrderComplainPresenter(OrderComplainContract.View view, OrderRepository orderRepository, UserRepository userRepository) {
        mView = view;
        mOrderRepository = orderRepository;
        mUserRepository = userRepository;
    }

    @Override
    public void subscribe() {
        super.subscribe();
        reqComplainStatus(); //获取投诉状态
        if (mFirstSubscribe) reqComplainMsg(); //获取投诉标签
    }

    @Override
    public void setOrderUuid(String orderUuid) {
        mOrderUuid = orderUuid;
    }

    @Override
    public String getOrderUuid() {
        return mOrderUuid;
    }

    @Override
    public void reqComplainStatus() {
        mSubscriptions.add(mOrderRepository.isComplain(mOrderUuid)
                .compose(RxUtil.applySchedulers())
                .doOnSubscribe(() -> mView.showLoadingView(true))
                .doAfterTerminate(() -> mView.hideLoadingView())
                .subscribe(entity -> {
                    int status = TypeUtils.getValue(entity.status);
                    switch (status) {
                        case 1:
                        case 2:
                        case 3:
                            mView.complained(mOrderUuid,
                                    TypeUtils.getValue(entity.contents),
                                    TypeUtils.getValue(entity.result),
                                    status != 3); //投诉处理中
                            break;
                    }
                }, ex -> {
                    showNetworkError(ex, R.string.network_error, mView, mUserRepository);
                }));
    }

    @Override
    public void reqComplainMsg() {
        mSubscriptions.add(mUserRepository.reqComplainMsg()
                        .flatMapIterable(entities -> entities)
                        .map(ComplainVO::createFrom)
                        .toList()
                        .compose(RxUtil.applySchedulers())
//                .doOnSubscribe(() -> mView.showLoadingView(true))
//                .doAfterTerminate(() -> mView.hideLoadingView())
                        .subscribe(vos -> {
                            mView.showComplainMsg(vos);
                        }, ex -> {
                            showNetworkError(ex, R.string.network_error, mView, mUserRepository);
                        })
        );
    }

    @Override
    public void submitComplain(final String content) {
        if (TextUtils.isEmpty(content)) {
            toast("请选择投诉原因");
            return;
        }
        mSubscriptions.add(mOrderRepository.reqComplainOrder(mOrderUuid, content, "")
                .compose(RxUtil.applySchedulers())
                .doOnSubscribe(() -> mView.showLoadingView(true))
                .doAfterTerminate(() -> mView.hideLoadingView())
                .subscribe(s -> {
                    mView.complainSuccess(mOrderUuid, content);
                }, ex -> {
                    showNetworkError(ex, R.string.network_error, mView, mUserRepository);
                }));
    }

    @Override
    public int driverType() {
        return mUserRepository.getDriverType();
    }
}
